'use strict';
const db = uniCloud.database();
var express = db.collection("express")
const querystring = require('querystring')
exports.main = async (event, context) => {
	var parse=querystring.parse(event.body)
	//首先查询一下任务id，若已有此任务了，则直接替换信息，没有的话再创建
	var row = await express.where({
		taskId:parse.taskId
	}).get();
	//如果不存在，那就直接添加
	if(row.data.length<1){
		// var res = await express.add(parse);
		//字符串类型的转成了对象类型
		let obj = JSON.parse(parse.param);
		//一会还需要taskId，所以这里要加上
		obj.taskId=parse.taskId;
		//把物流状态，在存入数据库前就由数字代表转成中文
		switch (obj.data.status/1){
			case 0:
				obj.data.status = '下单成功';
				break;
			case 1:
				obj.data.status = '已接单';
				break;
			case 2:
				obj.data.status = '收件中';
				break;
			case 9:
				obj.data.status = '用户主动取消';
				break;
			case 10:
				obj.data.status = '已取件';
				break;
			case 11:
				obj.data.status = '揽货失败';
				break;
			case 12:
				obj.data.status = '已退回';
				break;
			case 13:
				obj.data.status = '已签收';
				break;
			case 14:
				obj.data.status = '异常签收';
				break;
			case 99:
				obj.data.status = '订单已取消';
				break;
			case 101:
				obj.data.status = '在途中';
				break;
			default:
				break;
		}
		//把快递公司代码转成中文
		switch(obj.kuaidicom){
			case "jd":
			obj.kuaidicom = "京东物流";
			break;
			case "yuantong":
			obj.kuaidicom = "圆通速递";
			break;
			case "yunda":
			obj.kuaidicom = "韵达快递";
			break;
			case "huitongkuaidi":
			obj.kuaidicom = "百世(汇通)快递";
			break;
			case "shentong":
			obj.kuaidicom = "申通快递";
			break;
			case "zhaijisong":
			obj.kuaidicom = "宅急送";
			break;
			case "debangkuaidi":
			obj.kuaidicom = "德邦快递";
			break;
			case "youshuwuliu":
			obj.kuaidicom = "优速快递";
			break;
			case "zhongtongkuaiyun":
			obj.kuaidicom = "中通快运";
			break;
			case "yundakuaiyun":
			obj.kuaidicom = "韵达快运";
			break;
			case "yuantongkuaiyun":
			obj.kuaidicom = "圆通快运";
			case "shunfeng":
			obj.kuaidicom = "顺丰";
			
			break;
			default:
			break;
		}
		//添加进数据库
		var res = await express.add(obj);
	}
	//如果已经有了，就更新
	else{
		let obj = JSON.parse(parse.param);
		obj.taskId=parse.taskId;
		//把物流状态，在存入数据库前就由数字代表转成中文
		switch (obj.data.status/1){
			case 0:
				obj.data.status = '下单成功';
				break;
			case 1:
				obj.data.status = '已接单';
				break;
			case 2:
				obj.data.status = '收件中';
				break;
			case 9:
				obj.data.status = '用户主动取消';
				break;
			case 10:
				obj.data.status = '已取件';
				break;
			case 11:
				obj.data.status = '揽货失败';
				break;
			case 12:
				obj.data.status = '已退回';
				break;
			case 13:
				obj.data.status = '已签收';
				break;
			case 14:
				obj.data.status = '异常签收';
				break;
			case 99:
				obj.data.status = '订单已取消';
				break;
			case 101:
				obj.data.status = '在途中';
				break;
			default:
				break;
		}
		//把快递公司代码转成中文
		switch(obj.kuaidicom){
			case "jd":
			obj.kuaidicom = "京东物流";
			break;
			case "yuantong":
			obj.kuaidicom = "圆通速递";
			break;
			case "yunda":
			obj.kuaidicom = "韵达快递";
			break;
			case "huitongkuaidi":
			obj.kuaidicom = "百世(汇通)快递";
			break;
			case "shentong":
			obj.kuaidicom = "申通快递";
			break;
			case "zhaijisong":
			obj.kuaidicom = "宅急送";
			break;
			case "debangkuaidi":
			obj.kuaidicom = "德邦快递";
			break;
			case "youshuwuliu":
			obj.kuaidicom = "优速快递";
			break;
			case "zhongtongkuaiyun":
			obj.kuaidicom = "中通快运";
			break;
			case "yundakuaiyun":
			obj.kuaidicom = "韵达快运";
			break;
			case "yuantongkuaiyun":
			obj.kuaidicom = "圆通快运";
			break;
			case "shunfeng":
			obj.kuaidicom = "顺丰";
			default:
			break;
		}
		//根据查询条件，已经查出了数据库中已有记录的id
		var rid = row.data[0]._id;
		var res = await express.doc(rid).update(obj)
	}
	//返回数据给快递100（没用）
	return event
};
